package com.treasure.service.third.utils;

import com.treasure.service.commons.lang.util.StringUtil;
import com.treasure.service.enums.CodeEnum;
import com.treasure.service.enums.RedisKeys;
import com.treasure.service.result.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;



/**
* @Description:    Todo 公用发短信
* @Author:         panxinwen
* @CreateDate:     2018/11/18 17:52
* @Version:        1.0
*/
@Component
public class SmsUtils {

    @Autowired
    private RestClient restClient;

    @Autowired
    private RestTemplate restTemplate;

    @Value("${treasure.rest.service.third}")
    private String url;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public ResponseResult sendSms(String phonenumber, Integer type){

        Object time= redisTemplate.opsForHash().get(RedisKeys.AUTH_CODE.build(phonenumber),"A"+type);

        if (StringUtil.isNotEmpty(time)){
            Long now = Long.parseLong(StringUtil.getNowTimeStamp());
            Long timeStamp = Long.parseLong(time.toString());
            if (now-timeStamp<60){
                return new ResponseResult(CodeEnum.OFTEN);
            }
        }

//        Map<String, Object> requestEntity = new HashMap<>();
//        requestEntity.put("phonenumber",phonenumber);
//        requestEntity.put("smsType",type+"");
//
//        return restClient.post(url+"/sms/v1/sendSms",requestEntity,ResponseResult.class);

        MultiValueMap<String, Object> map = new LinkedMultiValueMap();
        map.set("phonenumber",phonenumber+"");
        map.set("smsType",type+"");

        HttpHeaders httpHeaders = new HttpHeaders();
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<MultiValueMap<String, Object>>(map, httpHeaders);
        ParameterizedTypeReference<ResponseResult> pt = new ParameterizedTypeReference<ResponseResult>() {
        };
        try {
            logger.error("请求方法 "+url + "/sms/v1/sendSms ");
            ResponseEntity<ResponseResult> resp = restTemplate.exchange(url + "/sms/v1/sendSms",
                    HttpMethod.POST, formEntity, pt);
            return resp.getBody();
        } catch (HttpClientErrorException e) {

            logger.error("出错了",e);
        }

        return null;

    }

    public ResponseResult sendSmsTemp(String phonenumber, String temp){


        MultiValueMap<String, Object> map = new LinkedMultiValueMap();
        map.set("phonenumber",phonenumber+"");
        map.set("temp",temp+"");

        HttpHeaders httpHeaders = new HttpHeaders();
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<MultiValueMap<String, Object>>(map, httpHeaders);
        ParameterizedTypeReference<ResponseResult> pt = new ParameterizedTypeReference<ResponseResult>() {
        };
        try {
            logger.error("请求方法 "+url + "/sms/v1/sendSmsTemp");
            ResponseEntity<ResponseResult> resp = restTemplate.exchange(url + "/sms/v1/sendSmsTemp",
                    HttpMethod.POST, formEntity, pt);
            return resp.getBody();
        } catch (HttpClientErrorException e) {

            logger.error("出错了",e);
        }

        return null;

    }

    public ResponseResult verify(String phonenumber, Integer type,String authCode,Long duration){

        Object time= redisTemplate.opsForHash().get(RedisKeys.AUTH_CODE.build(phonenumber),"A"+type);
        Object code= redisTemplate.opsForHash().get(RedisKeys.AUTH_CODE.build(phonenumber),""+type);

        ResponseResult responseResult = new ResponseResult();
        if (StringUtil.isEmpty(code) || !authCode.equals(code)){
            responseResult = new ResponseResult(CodeEnum.CODE_ERROR);
        }else if (StringUtil.isNotEmpty(time)){
            Long now = Long.parseLong(StringUtil.getNowTimeStamp());
            Long timeStamp = Long.parseLong(time.toString());
            if (now-timeStamp>300){
                responseResult = new ResponseResult(CodeEnum.PAST_DUE);
            }
        }

        return responseResult;

    }

}
